package me.pplay.playtalk.view;

import java.util.ArrayList;

import me.pplay.playtalk.R;
import me.pplay.playtalk.bean.Face;
import me.pplay.playtalk.data.Globals;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ImageView.ScaleType;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;

public class SmileyLayout extends FrameLayout implements OnCheckedChangeListener{

	private Context mContext;
//	private HorizontalScrollView mHorizontalScrollView;
//	private float mCurrentCheckedRadioLeft;
	private RadioGroup mRadioGroup;
	private RadioGroup radioGroup_stick;
//	private ImageView mImageView;
	private LinearLayout addPager, tab_stiker_layout;
	private RelativeLayout tab_stiker;
	private ArrayList<PagerLayout> mViews;
	public String[] columnList;
	private int width = 0;
	private int currentIndex = 0;
	private Handler mHandler;
	private Face face;
	
	
	public SmileyLayout(Context context,AttributeSet attrs) {
		super(context, attrs);
		mContext		= context;
		face			= Face.getInstance(context);
		columnList		= face.columns;//Face.getInstance(context).getColumnList();
		width 			= Globals.width/3;
		addView();
	}
	
	public SmileyLayout(Context context,Handler handler) {
		super(context);
		mContext		= context;
		mHandler		= handler;
		face			= Face.getInstance(context);
		columnList		= face.columns;//Face.getInstance(context).getColumnList();
		width 			= Globals.width/3;
		addView();
	}
	
	public void setHandler(Handler handler){
		mHandler		= handler;
	}
	
	private void addView(){
		View v					= LayoutInflater.from(mContext).inflate(R.layout.layout_smiley, null);
		mRadioGroup				= (RadioGroup)v.findViewById(R.id.radioGroup);
		radioGroup_stick		= (RadioGroup)v.findViewById(R.id.radioGroup_stick);
		tab_stiker_layout		= (LinearLayout)v.findViewById(R.id.tab_stiker_layout);
		addPager				= (LinearLayout)v.findViewById(R.id.addPager);
		tab_stiker				= (RelativeLayout)v.findViewById(R.id.tab_stiker);
		
		this.addView(v, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		iniController();
		iniListener();
	}
	
	private void iniController(){
		tab_stiker.setVisibility(View.GONE);
		mViews 			= new ArrayList<PagerLayout>();
		int count 		= columnList.length;
		for(int i = 0; i < count; i++){
			RadioButton rdo = new RadioButton(mContext);
			rdo.setText(columnList[i]);
			rdo.setId(i);
			setRadioGroupStyle(rdo, i);
			mRadioGroup.addView(rdo);
			PagerLayout pageLayout = null;
			if(i < 2){
				pageLayout = new PagerLayout(mContext, i, face.faceNamesList.get(i), mHandler);
				addPager.addView(pageLayout);
				mViews.add(pageLayout);
			}
			
		}
		
		int w = BitmapFactory.decodeResource(getResources(), R.drawable.tab_sticker_on).getWidth();
		for (int i = 0; i < face.stickMap.size(); i++) {
			
			LinearLayout layout = new LinearLayout(mContext);
			layout.setLayoutParams(new LinearLayout.LayoutParams(w, LayoutParams.WRAP_CONTENT));
			layout.setGravity(Gravity.CENTER);
			ImageView imageView = new ImageView(mContext);
			imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			imageView.setScaleType(ScaleType.CENTER);
			RadioButton rdo = new RadioButton(mContext);
			rdo.setId(count + i);
			rdo.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		    rdo.setGravity(Gravity.CENTER);
		    rdo.setButtonDrawable(null);
		    rdo.setEllipsize(TruncateAt.MARQUEE);
		    rdo.setSingleLine(true);
			if(i == 0){
				imageView.setBackgroundResource(R.drawable.tab_bear_icon);
				rdo.setBackgroundResource(R.drawable.tab_sticker_on);
			}else{
				rdo.setBackgroundResource(R.drawable.tab_sticker);
				imageView.setBackgroundResource(R.drawable.tab_rabbit_icon);
			}
			layout.addView(imageView);
			tab_stiker_layout.addView(layout);
			radioGroup_stick.addView(rdo);
			PagerLayout pageLayout = new PagerLayout(mContext, count, face.stickMap.get(i), mHandler);
			addPager.addView(pageLayout);
			mViews.add(pageLayout);
		}
	}

	private void iniListener() {
		mRadioGroup.setOnCheckedChangeListener(this);
		radioGroup_stick.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				System.out.println("*****——++++++++++++++"+currentIndex + "   "+checkedId + "   "+mViews.size());
				int count 		= columnList.length;
				int facelength 	= face.stickMap.size();
				for(int i = count; i < (count + facelength); i++){
					RadioButton rdo = (RadioButton)group.findViewById(i);
					if(rdo == null)continue;
					if(i == checkedId)
						rdo.setBackgroundResource(R.drawable.tab_sticker_on);
					else
						rdo.setBackgroundResource(R.drawable.tab_sticker);
				}
				currentIndex 	= checkedId - 1;
				addPager.removeAllViews();
				PagerLayout pageLayout = mViews.get(currentIndex);
				if(pageLayout == null){
					pageLayout = new PagerLayout(mContext, currentIndex, face.faceNamesList.get(currentIndex), mHandler);
					mViews.add(pageLayout);
				}
				addPager.addView(pageLayout);
			}
		});
	}
	

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		currentIndex 	= checkedId;
		int count 		= columnList.length;
		if(currentIndex == 2)
			tab_stiker.setVisibility(View.VISIBLE);
		else
			tab_stiker.setVisibility(View.GONE);
		
		for(int i = 0; i < count; i++){
			RadioButton rdo = (RadioButton)group.findViewById(i);
			if(i == currentIndex)
				rdo.setBackgroundResource(R.drawable.rdo_smile_on);
			else
				rdo.setBackgroundResource(R.drawable.rdo_smile);
		}
		

		addPager.removeAllViews();
		PagerLayout pageLayout = mViews.get(currentIndex);
		if(pageLayout == null){
			pageLayout = new PagerLayout(mContext, currentIndex, face.faceNamesList.get(currentIndex), mHandler);
			mViews.add(pageLayout);
		}
		addPager.addView(pageLayout);
	}
	
	private void setRadioGroupStyle(RadioButton rdo, int index){
		 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LayoutParams.WRAP_CONTENT);
		 params.weight	= (float)1.0;
		 rdo.setLayoutParams(params);
		 rdo.setPadding(10, 10, 10, 10);
		 rdo.setGravity(Gravity.CENTER);
		 rdo.setButtonDrawable(null);
		 rdo.setEllipsize(TruncateAt.MARQUEE);
		 rdo.setSingleLine(true);
		 if(index == 0)
			 rdo.setBackgroundResource(R.drawable.rdo_smile_on);
		 else
			 rdo.setBackgroundResource(R.drawable.rdo_smile);
	}
	
}




